package com.yingside.jdbc.crm;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DeptDao {
    public List<Dept> list(){
        Connection conn = null;
        Statement stmt = null;
        //结果集(游标)
        ResultSet rs = null;


        List<Dept> list = new ArrayList<Dept>();

        try {
            //3.获取连接
            conn = DBHelper.getConn();

            //4.获取stmt对象
            stmt = conn.createStatement();

            //5.执行sql语句
            String sql = "select id,dept_name,dept_info,create_time from t_dept";

            rs = stmt.executeQuery(sql);

            while(rs.next()){

                Dept dept = new Dept();
                dept.setId(rs.getInt("id"));
                dept.setDeptName(rs.getString("dept_name"));
                dept.setDeptInfo(rs.getString("dept_info"));
                dept.setCreateTime(rs.getString("create_time"));

                list.add(dept);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.close(rs,stmt,conn);
        }

        return list;
    }
}
